<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>房间类型管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../../layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <!--                <div class="layui-inline">-->
                <!--                    <label class="layui-form-label">类型名称</label>-->
                <!--                    <div class="layui-input-block">-->
                <!--                        <select class="layui-input-block" id="room_product_type_id" lay-filter="room_product_type_id">-->
                <!--                        </select>-->
                <!--                    </div>-->
                <!--                </div>-->

                <div class="layui-inline">
                    <label class="layui-form-label">可住人数</label>
                    <div class="layui-input-block">
                        <select class="layui-input-block" id="peoplenumber" lay-filter="peoplenumber">
                            <option value="0">全部</option>
                            <option value="1">1</option>
                            <option value="2">2</option>
                            <option value="3">3</option>
                            <option value="4">4</option>
                        </select>
                    </div>
                </div>

                <div class="layui-inline">
                    <label class="layui-form-label">范围</label>
                    <div class="layui-input-inline" style="width: 100px;">
                        <input type="text" id="price_min" placeholder="￥" autocomplete="off" class="layui-input">
                    </div>
                    <div class="layui-form-mid">-</div>
                    <div class="layui-input-inline" style="width: 100px;">
                        <input type="text" id="price_max" placeholder="￥" autocomplete="off" class="layui-input">
                    </div>
                </div>


                <!-- 搜索按钮 -->
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-list" id="employee_search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>

                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-role" data-type="add">添加</button>
                </div>
            </div>
        </div>
        <div class="layui-card-body">
            <table id="Room_typeTable" lay-filter="Room_typeTable"></table>
        </div>
    </div>
</div>
<button type="button" id="importModel" class="layui-hide"></button>
<script type="text/html" id="table_bar">
    <div class="layui-btn-container">
        <a class="layui-btn layui-btn-xs" lay-event="updateIcon"><i class="layui-icon">&#xe67c;</i>宣传图</a>
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </div>
</script>

<script src="../../js/jquery-1.8.3.min.js"></script>
<script src="../../layuiadmin/layui/layui.js"></script>
<script>

    //路径配置 - 手动填写
    var findPageURL = "/roomType/findPage";
    var addURL = "/roomType/add";
    var toUpdateURL = "/roomType/findOne";
    var updateURL = "/roomType/update";
    var deleteURL = "/roomType/delete";

    var formTitle = "房间类型";
    //页面配置 - 不用填写
    var Room_typeType = "";
    var Room_typeURL = "";
    var Room_typeId = "";
    var Room_Name = "";

    // //下拉框初始化
    // $.ajax({
    //     url: '/roomType/toAdd',
    //     type: 'post',
    //     dataType: 'json',
    //     data: {},
    //     async: false,//设置阻断
    //     success: function (data) {
    //         //将下拉框中添加数据
    //         var modelList = data.room_product_type;
    //         if (modelList && modelList.length != 0) {
    //             var dep = "<option value='-1'>全部</option>";
    //             $("#room_product_type_id").append(dep);
    //             for (var i = 0; i < modelList.length; i++) {
    //                 var option = "<option value=\"" + modelList[i].id + "\"";
    //                 option += ">" + modelList[i].name + "</option>";  //动态添加数据
    //                 $('#room_product_type_id').append(option);
    //             }
    //         }
    //     }
    // });
    var uploadInst;
    var iconId = 0;

    function updataIcon(id) {
        iconId = id;
        //重载该实例，支持重载全部基础参数
        uploadInst.reload({
            data: {pathId: id}
        });
        $("#importModel").click();
    }

    //layui配置
    layui.config({
        base: '../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'useradmin', 'table', 'util', 'upload'], function () {
        var $ = layui.$
            , form = layui.form
            , table = layui.table
            , layer = layui.layer
            , util = layui.util
            , upload = layui.upload;


        //监听搜索
        $("#employee_search").on('click', function (data) {

            //执行重载
            table.reload('Room_typeTable', {
                method: 'post'
                , where: {
                    // "room_product_type_id": $("#room_product_type_id").val() == "" ? 0 : $("#room_product_type_id").val(),
                    "peoplenumber": $("#peoplenumber").val() == "" ? 0 : $("#peoplenumber").val(),
                    "price_min": $("#price_min").val() == "" ? 0 : $("#price_min").val(),
                    "price_max": $("#price_max").val() == "" ? 0 : $("#price_max").val()
                }
                , page: {curr: 1}
            });
        });

        //表格执行渲染
        table.render({
            elem: '#Room_typeTable'
            , id: 'Room_typeTable'
            , url: findPageURL
            , page: true
            , limits: [10, 20, 30, 50]
            , cols: [[
                {fixed: 'left', width: 110, field: 'name', title: '房间类型'}
                , {
                    field: 'icon', title: '宣传图', width: 90, templet: function (d) {
                        if (d.icon != null) {
                            return "<img src=\"/download/" + d.icon + "\" width=\"28px\" height=\"28px\">"
                        }
                        return "<img src=\"/download/icon.jpg\" width=\"28px\" height=\"28px\">"
                    }
                }
                , {field: 'price', title: '单日价格(元)', width: 110}
                , {field: 'floors', title: '楼层', width: 80}
                , {field: 'size', title: '房间大小', width: 100}
                , {field: 'bednumber', title: '床数', width: 80}
                , {field: 'peoplenumber', title: '可住人数', width: 100}
                , {
                    field: 'iswindow', title: '是否有窗', width: 100, templet: function (d) {
                        if (d.iswindow == "1") return "<span class=\"layui-badge\">有</span>"
                        else return "<span class=\"layui-badge layui-bg-gray\">无</span>"
                    }
                }
                , {
                    field: 'iswindow', title: '免费WIFI', width: 100, templet: function (d) {
                        if (d.iswifi == "1") return "<span class=\"layui-badge\">有</span>"
                        else return "<span class=\"layui-badge layui-bg-gray\">无</span>"
                    }
                }
                , {
                    field: 'iswindow', title: '高速宽带', width: 100, templet: function (d) {
                        if (d.isweb == "1") return "<span class=\"layui-badge\">有</span>"
                        else return "<span class=\"layui-badge layui-bg-gray\">无</span>"
                    }
                }
                // , {field: 'rptname', title: '房内物品类别', width: 120}
                , {title: '操作', fixed: 'right', width: 180, align: 'center', toolbar: '#table_bar'}
            ]]
            ,
            where: {
                // "room_product_type_id":
                //     $("#room_product_type_id").val() == "" ? 0 : $("#room_product_type_id").val(),
                "peoplenumber":
                    $("#peoplenumber").val() == "" ? 0 : $("#peoplenumber").val(),
                "price_min":
                    $("#price_min").val() == "" ? 0 : $("#price_min").val(),
                "price_max":
                    $("#price_max").val() == "" ? 0 : $("#price_max").val()
            }
        });

        //添加操作
        var $ = layui.$, active = {
            add: function () {
                Room_typeType = "add";
                Room_typeURL = addURL;
                layer.open({
                    type: 2
                    , title: "添加" + formTitle
                    , content: 'form.html'
                    , maxmin: true
                    , area: ['430px', '500px']
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        //点击确认触发 iframe 内容中的按钮提交
                        var submit = layero.find('iframe').contents().find("#employee-submit");
                        submit.click();
                    }

                });
            }
        };
        var load;
        // 初始化文件上传组件
        uploadInst = upload.render({
            elem: "#importModel" //绑定元素
            , accept: 'images'
            , acceptMime: 'image/*'
            , method: 'post'
            , size: 1024
            , url: "/roomType/uploadIcon?id=" + iconId
            , before: function (obj) { //obj参数包含的信息，跟 choose回调完全一致，可参见上文。
                //上传loading
                load = layer.load(1, {
                    shade: [0.3, '#fff'] //0.1透明度的白色背景
                });
            }
            , done: function (res, index, upload) {
                layer.close(load);
                layer.msg(res.msg, {icon: res.icon, shade: 0.3, time: 1500}, function (index) {
                    location.reload();
                });

            }
            , error: function () {
                layer.close(load);
                layer.msg("连接失败！")
            }
        });
        //监听行工具事件
        table.on('tool(Room_typeTable)', function (obj) { //注：tool 是工具条事件名，zq_table 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                , layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象
            switch (layEvent) {
                case 'del':
                    layer.confirm('您确定删除id为：' + data.id + '的数据吗？', function (index) {
                        //向服务端发送删除指令，在这里可以使用Ajax异步
                        $.post(deleteURL, {id: data.id}, function (ret) {
                            layer.msg(ret.msg, {icon: ret.icon, time: 1500});
                            if (ret.status == "1") {//删除成功，刷新当前页表格
                                obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                                layer.close(index);
                                $(".layui-laypage-btn").click();//点击分页刷新当前页
                            }
                        });
                    });
                    break;
                case 'edit':
                    Room_typeType = "toUpdate";
                    Room_typeURL = updateURL;
                    Room_typeId = data.id;
                    Room_Name = data.name;
                    layer.open({
                        type: 2
                        , title: "修改" + formTitle
                        , content: 'form.html'
                        , maxmin: true
                        , area: ['430px', '500px']
                        , btn: ['确定', '取消']
                        , yes: function (index, layero) {
                            //点击确认触发 iframe 内容中的按钮提交
                            var submit = layero.find('iframe').contents().find("#employee-submit");
                            submit.click();
                        }
                    });
                    break;
                case 'updateIcon':
                    updataIcon(data.id);
                    break;
            }
        });


        //必备
        $('.layui-btn.layuiadmin-btn-role').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });


</script>
</body>
</html>

